var grid;
var form;
var form1;

$(function () {

    //加载该页面前，先判断该模块的权限
    //获取当前登录的用户的权限
    var permission = document.cookie.split(";")[1].split("=")[1];
    var b = String(permission).split('');
    for (var i = 0; i < b.length; i++) {
        console.log(b[i]);
    }

    var moudle51 = b[15];
    //如果模块权限为1，即只能查询此模块下的数据，不能进行修改（禁用和重置密码不能使用）
    //系统管理
    if (moudle51 == 1) {
        $("#addNewBtn").css('display', 'none');
    }

    var pageNo = 1;
    var pageSize = 5;
    var grid_selector = "#grid-table";
    var pager_selector = "#grid-pager";
    //初始化数据表格
    grid = $(grid_selector).jqGrid({
        datatype: "json",
        url: 'getRoleList',
        mtype: 'POST',
        page: pageNo,
        postData: {
            'rooname': $("#RRoleName").val(),
            'bankname': $("#RCompany option:selected").val(),
        },
        height: 380,
        width: 300,
        colNames: ['', "角色名称", "所属银行", "描述"],
        colModel: [
            {
                name: 'astate',
                index: 'astate',
                width: 70,
                align: 'center',
                sortable: false,
                formatter: function (cellvalue, options, rowObject) {
                    if (flag == false) {
                        return "<button disabled class='btn btn-danger btn-xs' onclick='openDelete(\"" + rowObject.id + "\")'><i class='fa fa-trash button-in-i'></i>删除</button>";
                    } else {
                        return "<button class='btn btn-danger btn-xs' onclick='openDelete(\"" + rowObject.id + "\")'><i class='fa fa-trash button-in-i'></i>删除</button>";
                    }
                }
            },
            {name: 'ROONAME', index: 'ROONAME', align: 'center', sortable: false,},
            {name: 'BANKNAME', index: 'BANKNAME', align: 'center', sortable: false,},
            {name: 'description', index: 'description', align: 'center', sortable: false,},
        ],
        viewrecords: true,
        rowNum: pageSize,
        rownumbers: true,//添加左侧行号
        rowList: [5, 10, 15],
        pager: pager_selector,
        altRows: true,
        multiselect: false,
        loadComplete: function (data) {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
            }, 0);
            var h = $(window).height() - ($('#grid-pager')[0].offsetHeight + 15) - ($('.result-table .box')[0].offsetTop + $('.ui-jqgrid-bdiv')[0].offsetTop + 3);
            $('.ui-jqgrid-bdiv').height(h);
        },
        autowidth: true,
        jsonReader: jsonReader,   // json中代表实际模型数据的入口
        rownumWidth: 52,
        prmNames: prmNames,
        recordtext: "显示 {0} - {1} 条 , 共 {2} 条",
        emptyrecords: "没有记录",
        loadtext: "正在加载...",
        pgtext: "第 {0} 页  ,共 {1} 页"
    });
    //初始化分页条
    /*showNavGrid(grid_selector, pager_selector);*/

    //查询
    $("#queryBtn").click(function () {
        var filters = {
            'rooname': $("#RRoleName").val(),
            'bankname': $("#RCompany option:selected").val(),
        }
        grid.jqGrid("setGridParam", {postData: filters, page: 1});
        grid.trigger('reloadGrid');
    });
    //加载所属银行
    var selectCompany = $("#RCompany");//银行
    $.ajax({
        url: "getAllComOrgRole",
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                var company = data.allBankName;//银行
                selectCompany.html("");
                //向银行select传入数据
                var options = "<option value=\"0\">--请选择--</option>";
                for (var i = 0; i < company.length; i++) {
                    options += "<option value=\"" + company[i].id + "\">" + company[i].bankname + "</option>";
                }
                selectCompany.html(options);
            }
        }
    })


});

//删除记录
function openDelete(obj) {
    bootbox.confirm("您确定要将删除此行记录吗?", function (result) {
        if (result) {
            $.post("deleteRoleById", {
                "id": obj,
            }, function (data) {
                if (data.deleteResult == 1) {
                    alert("角色删除成功！");
                } else {
                    alert("角色删除失败！");
                }
                grid.trigger('reloadGrid');
            });
        }
    });
}

//清空
function resetQuery() {
    $("#RRoleName").val("");
    $("#RCompany option:first").prop("selected", 'selected');
}